package frontend;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;

public class ServidorFront {

	private String ipServerDNS = "localhost";

	private int puertoDNS = 5002;
    private ObjectInputStream flujoEntradaDNS;
    private ObjectOutputStream flujoSalidaDNS;

    private Socket socketDNS;
    private Object TS = new Integer(0);
    private Object Cordinador = new Boolean(false);
    List<Socket> listaClientesFront; 
    ServerSocket socketServerFront;
    int puertoFrontEnd;
            
    public ServidorFront(int frontPort) throws UnknownHostException, IOException {
    	listaClientesFront = new ArrayList<Socket> ();
        this.puertoFrontEnd = frontPort;


        System.out.println("FrontEnd: Conectando a BackEnd DNS");
        this.socketDNS = new Socket(ipServerDNS,puertoDNS);
        this.flujoSalidaDNS = new ObjectOutputStream(socketDNS.getOutputStream());
        this.flujoEntradaDNS= new ObjectInputStream(socketDNS.getInputStream());
    }
    
    public void iniciar () throws IOException {
        socketServerFront = new ServerSocket (puertoFrontEnd);
        System.out.println("FrontEnd: Servidor FrontEnd iniciado");
        this.flujoSalidaDNS.writeUTF("askCord");
        this.flujoSalidaDNS.flush();
        try {
			String msj = (String) this.flujoEntradaDNS.readObject();
			if ("YC".equalsIgnoreCase(msj)) {
				System.out.println("FrontEnd: Elegido como cordinador");
			}
			else {
				System.out.println("FrontEnd: No es cordinador");
			}
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        while (true) {
            try {
            	Socket socketClienteFront = socketServerFront.accept();
                listaClientesFront.add(socketClienteFront);
                ThreadClienteFront clienteFront;
				clienteFront = new ThreadClienteFront(socketClienteFront,this.listaClientesFront, 
						                              this.flujoEntradaDNS, this.flujoSalidaDNS,
						                              this.TS, this.Cordinador);
                clienteFront.start();
                System.out.println("FrontEnd: Nuevo Cliente Conectado");
            }
            catch (IOException e) {
                System.out.println("FrontEnd: Error al inicial FrontEnd");
                this.desconectar();
                e.printStackTrace();
            }
        }
    }

	private void desconectar() throws IOException {
        flujoEntradaDNS.close();
        flujoSalidaDNS.close();
        socketDNS.close();
    }

	public static void main(String args[]) throws UnknownHostException, IOException {
		System.out.println("FrontEnd: Inicializando Servidor FrontEnd");
		ServidorFront servidor = new ServidorFront(1235);
    	try {
			servidor.iniciar();
		} catch (IOException e) {
			e.printStackTrace();
		}
    }
}
